package com.fhsk.file.manager.controller;

import com.fhsk.file.business.entity.bo.user.UserBo;
import com.fhsk.file.business.entity.bo.user.UserConditionBo;
import com.fhsk.file.business.entity.vo.user.UserVo;
import com.fhsk.file.business.service.IUserService;
import com.fhsk.file.common.enums.RedisKeyEnum;
import com.fhsk.file.common.utils.RedisComponent;
import com.fhsk.file.common.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "", tags = "用户管理")
public class UserController {

    private final String ACCESS_TOKEN = "Authorization";

    @Autowired
    HttpServletRequest request;

    @Resource
    private IUserService userService;

    @Resource
    private RedisComponent redisComponent;

    @Value("${loginSingle}")
    private boolean loginSingle;

    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息，由前端控制跳转页面
     *
     * @return
     */
    @RequestMapping(value = "/unauthenticated")
    public AjaxResult unauth() {
        return AjaxResult.fail(406, "未登录!");
    }

    /**
     * 权限不足，shiro应重定向到403界面，此处返回权限不足信息，由前端控制跳转页面
     *
     * @return
     */
    @RequestMapping(value = "/unauthorized")
    public AjaxResult unauthorized() {
        return AjaxResult.fail(403, "权限不足!");
    }

    @ApiOperation("登出接口,在缓存中删除改token")
    @RequestMapping(value = "/logout")
    public AjaxResult logout(@RequestHeader(ACCESS_TOKEN) String token) {
        String account = userService.getCurrentUser().getAccount();
        String tokenKey = RedisKeyEnum.TOKEN.getValue() + account;
        if (loginSingle) {
            //如果互顶为开,则删除当前用户的全部token
            redisComponent.del(tokenKey);
        } else {
            //互顶为关,只移除当前的token
            redisComponent.setRemove(tokenKey, token);
        }
        return AjaxResult.success();
    }

    @ApiOperation("通过token获取用户信息")
    @GetMapping("/getUserByToken")
    public AjaxResult getUserByToken() {
        UserVo user = userService.getCurrentUser();
        //密码置空
        user.setPassword(null);
        return AjaxResult.success(user);
    }

    @ApiOperation("修改密码")
    @PostMapping("/changePassword")
    public AjaxResult changePassword(@RequestBody UserBo userBo) {
        Boolean update = userService.changePassword(userBo.getAccount(), userBo.getOldPassword(), userBo.getPassword());
        if (update) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("修改密码失败");
    }

    @ApiOperation("重置密码")
    @GetMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestParam("userOid") String userOid) {
        return userService.resetPassword(userOid);
    }

    @ApiOperation("查询用户")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody UserConditionBo conditionBo) {
        return AjaxResult.success(userService.list(conditionBo));
    }

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public AjaxResult addUser(@RequestBody UserBo userBo) {
        return userService.addUser(userBo);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetail")
    public AjaxResult getDetail(@RequestBody UserBo userBo) {
        return AjaxResult.success(userService.getDetail(userBo));
    }

    /**
     * 修改用户
     *
     * @param userBo
     * @return
     */
    @ApiOperation("修改用户")
    @PostMapping("/update")
    public AjaxResult updateUser(@RequestBody UserBo userBo) {
        if (null == userBo.getOid()) {
            return AjaxResult.fail("用户oid不能为空");
        }
        return userService.updateUser(userBo);
    }
}
